﻿package net.wangit.context;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import net.wangit.framework.action.ActionMapping;
import net.wangit.framework.session.AgileSession;
import net.wangit.util.Checker;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * ActionContext帮助类
 * @author watson
 *
 */
public class ActionContextHelper {

	private static final Log log = LogFactory
			.getLog(net.wangit.context.ActionContextHelper.class);
	private static final String ACTION_DOMAIN = "ActionContext.domain";
	private static final String ACTION_UNIT_ID = "ActionContext.auid";
	private static final String ACTION_ID = "ActionContext.aid";
	private static final String ACTION_DATASOURCE = "ActionContext.ad";

	private ActionContextHelper() {
	}

	public static String getRequest2Session(ActionContext ac,
			String requestName, String sessionName, String defaultValue) {
		String sessionKey = Checker.isEmpty(sessionName) ? requestName
				: sessionName;
		String value = ac.getRequestParameter(requestName);
		if (value == null)
			value = ac.getSessionAttribute(sessionKey, defaultValue);
		ac.setSessionAttribute(sessionKey, value);
		return value;
	}

	public static AgileSession getSession(ActionContext ac) {
		return (AgileSession) ac.getSessionAttribute(AgileSession.KEY);
	}

	public static String getActionDomain(ActionContext ac) {
		return (String) ac.getAttribute(ActionContextHelper.ACTION_DOMAIN);
	}

	public static String getActionUnitId(ActionContext ac) {
		return (String) ac.getAttribute(ActionContextHelper.ACTION_UNIT_ID);
	}

	public static String getActionId(ActionContext ac) {
		return (String) ac.getAttribute(ActionContextHelper.ACTION_ID);
	}

	public static String getActionDatasource(ActionContext ac) {
		return (String) ac.getAttribute(ActionContextHelper.ACTION_DATASOURCE);
	}

	public static void setMappingParameter(ActionContext ac, ActionMapping am) {
		ac.setAttribute(ActionContextHelper.ACTION_DOMAIN, am.getDomain());
		ac.setAttribute(ActionContextHelper.ACTION_UNIT_ID, am.getUnitId());
		ac.setAttribute(ActionContextHelper.ACTION_ID, am.getId());
		String datasource = am.getDatasource();
		if (!Checker.isEmpty(datasource))
			ac.setAttribute(ActionContextHelper.ACTION_DATASOURCE, datasource);
	}

	public static final void update() {
		LocalContext lc = LocalContext.getLocalContext();
		ActionContext ac = lc.getActionContext();
		HttpSession httpSession = lc.getHttpServletRequest().getSession();
		httpSession.setAttribute(AgileSession.KEY, getSession(ac));
		Map<String, Object> map = ((ActionContextImpl) ac).getSessionMap2();
		Set<String> keySet = map.keySet();
		String key;
		Object value;
		Iterator<String> keys = keySet.iterator();
		while (keys.hasNext()) {
			key = keys.next();
			value = map.get(key);
			if (log.isDebugEnabled())
				log.debug((new StringBuilder("put session : key=")).append(key)
						.append(" value=").append(value).toString());
			httpSession.setAttribute(key, value);
		}
	}

	public static void clearSession(ActionContext ac) {
		((ActionContextImpl) ac).clearSession();
	}

}
